package com.junzhiit.nengji.shop.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.shop.bo.FullReductionListBO;
import com.junzhiit.nengji.shop.dao.FullReductionDAO;
import com.junzhiit.nengji.shop.model.FullReduction;

@Repository
public class FullReductionDAOImpl extends AbstractBaseDAO<FullReduction>implements FullReductionDAO {

	@Override
	public List<FullReductionListBO> findAll(FullReductionListBO frlb,Integer pageNumber,Integer maxPageRow) {
		StringBuffer sql=new StringBuffer();
		StatementParameter param=new StatementParameter();
		sql.append("select");
		sql.append(" fr.*,count(o.pay_money) total_pay_money,count(o.id) total_orders_count,count(u.id) total_pay_user_count");
		sql.append(" from full_reduction fr");
		sql.append(" inner join full_reduction_condition frc on fr.id=frc.full_reduction_id");
		sql.append(" left join full_reduction_goods frg on fr.id=frg.full_reduction_id");
		sql.append(" left join goods g on frg.goods_id=g.id");
		sql.append(" left join orders_goods og on g.id=og.goods_id");
		sql.append(" left join orders o on og.order_id=o.id");
		sql.append(" left join user u on o.user_id=u.id");
		sql.append(" where fr.is_delete=? and fr.cs_id=? and fr.start_time between ? and ?");
		param.setInt(IsDelete.NO. toIntValue());
		param.setString(frlb.getCsId());
		param.setDate(frlb.getStartTime());
		param.setDate(frlb.getEndTime());
		if(StringUtils.isNotBlank(frlb.getName())){
			sql.append(" and fr.name like ?");
			param.setString("%"+frlb.getName()+"%");
		}
		if(frlb.getStatus()!=null){
			sql.append(" and fr.status=?");
			param.setInt(frlb.getStatus());
		}
		sql.append(" group by fr.id");
		return super.queryForPagingList(sql.toString(), FullReductionListBO.class, param, pageNumber,maxPageRow);
	}

	@Override
	public Integer getTotalCount(FullReductionListBO frlb) {
		StringBuffer sql=new StringBuffer();
		StatementParameter param=new StatementParameter();
		sql.append("select");
		sql.append(" count(fr.id)");
		sql.append(" from full_reduction fr");
		sql.append(" inner join full_reduction_condition frc on fr.id=frc.full_reduction_id");
		sql.append(" left join full_reduction_goods frg on fr.id=frg.full_reduction_id");
		sql.append(" left join goods g on frg.goods_id=g.id");
		sql.append(" left join orders_goods og on g.id=og.goods_id");
		sql.append(" left join orders o on og.order_id=o.id");
		sql.append(" left join user u on o.user_id=u.id");
		sql.append(" where fr.is_delete=? and fr.cs_id=? and fr.start_time between ? and ?");
		param.setInt(IsDelete.NO. toIntValue());
		param.setString(frlb.getCsId());
		param.setDate(frlb.getStartTime());
		param.setDate(frlb.getEndTime());
		if(StringUtils.isNotBlank(frlb.getName())){
			sql.append(" and fr.name like ?");
			param.setString("%"+frlb.getName()+"%");
		}
		if(frlb.getStatus()!=null){
			sql.append(" and fr.status=?");
			param.setInt(frlb.getStatus());
		}
		return super.queryForInt(sql.toString(),param);
	}

}
